package org.devbg.academy.corejava.shop.db.daos;

import java.util.List;

import org.devbg.academy.corejava.shop.db.entities.BasePojo;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class BaseTreeDAO<E extends BasePojo> extends BaseDAO<E> {

	private String parentProperty;

	public BaseTreeDAO(Class<E> clazz, String parentProperty) {
		super(clazz);
		this.parentProperty = parentProperty;
	}

	@SuppressWarnings("unchecked")
	public List<E> findRootEntities() {
		Criteria findRootEntities = this.createCriteria().add(
				Restrictions.isNull(parentProperty));
		List<E> result = findRootEntities.list();
		return result;
	}

	public int countRootEntities() {
		Criteria findRootEntities = this.createCriteria().add(
				Restrictions.isNull(parentProperty)).setProjection(
				Projections.rowCount());
		Integer result = (Integer) findRootEntities.uniqueResult();
		return result;
	}

}
